﻿/// <reference path="MicrosoftAjax.js"/>
/// <reference path="MicrosoftAjaxTemplates.js"/>

function onMyCommand(sender, args) {
    if (args.get_commandName() === "select") {

    }
    else if (args.get_commandName() === "edit") {
        var selectedIndex = args.get_commandArgument();
        var lastIndex = sender.get_selectedIndex();

        Sys.get("#viewTemplate" + lastIndex).style.display = "block";
        Sys.get("#editTemplate" + lastIndex).style.display = "none";

        Sys.get("#viewTemplate" + selectedIndex).style.display = "none";
        Sys.get("#editTemplate" + selectedIndex).style.display = "block";
    }
    else if (args.get_commandName() === "delete") {
        if (confirm('Are You sure?')) {
            alert('data successfully deleted...!! (just kidding :p)');
        }
    }
    else if (args.get_commandName() === "save") {
        if (confirm('Are You sure?')) {
            var selectedIndex = args.get_commandArgument();
            var dataItem = sender.findContext(args.get_commandSource()).dataItem;
            
            dataItem["Name"] = Sys.get("#editName" + selectedIndex).value;
            dataItem["ProductModel"] = Sys.get("#editProductModel" + selectedIndex).value;
            dataItem["Description"] = Sys.get("#editDescription" + selectedIndex).value;
            dataItem["Price"] = Sys.get("#editPrice" + selectedIndex).value;
            dataItem["ModifiedDate"] = Sys.get("#editModifiedDate" + selectedIndex).value;

            alert('data successfully saved...!!');

            Sys.get("#viewTemplate" + selectedIndex).style.display = "block";
            Sys.get("#editTemplate" + selectedIndex).style.display = "none";
        }
    }
    else if (args.get_commandName() === "cancel") {
        var selectedIndex = args.get_commandArgument();
        Sys.get("#viewTemplate" + selectedIndex).style.display = "block";
        Sys.get("#editTemplate" + selectedIndex).style.display = "none";
    }
}